package atguigu.day01;


import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

class ticket2{
    private int num=49;
    private int num2=1;

//    public synchronized void sale(){
//        if (num2<=50){
//            System.out.println(Thread.currentThread().getName()+"已经卖出第"+(num2++)+"张票，还剩"+(num--));
//        }
//    }
    Lock lock = new ReentrantLock();
    public void sale(){
        lock.lock();
        try {
            if (num2<=50){
            System.out.println(Thread.currentThread().getName()+"已经卖出第"+(num2++)+"张票，还剩"+(num--));
        }
        }finally {
            lock.unlock();
        }
    }

}


public class Ticket {
    public static void main(String[] args) {
        final ticket2 ticket2 = new ticket2();

//        new Thread(new Runnable() {
//            public void run() {
//                for (int i = 0; i <=50 ; i++) {
//                    ticket2.sale();
//                }
//            }
//        },"线程1").start();
//
//        new Thread(new Runnable() {
//                    public void run() {
//                        for (int i = 0; i <=50 ; i++) {
//                            ticket2.sale();
//                        }
//                    }
//                },"线程2").start();
//
//        new Thread(new Runnable() {
//                    public void run() {
//                        for (int i = 0; i <=50 ; i++) {
//                            ticket2.sale();
//                        }
//                    }
//                },"线程3").start();



        new Thread(() -> {for (int i = 0; i <=50 ; i++) ticket2.sale();},"线程1").start();
        new Thread(() -> {for (int i = 0; i <=50 ; i++) ticket2.sale();},"线程2").start();
        new Thread(() -> {for (int i = 0; i <=50 ; i++) ticket2.sale();},"线程3").start();

    }
}
